//首页幻灯片  by yangyong
define(function(require){
	var $=require("jq");
	function Slide(obj){
		//保存单个对象
		this.slide=obj;
		//保存所有幻灯片
		this.slideItems=$(".slide-box li",obj);	
		//保存幻灯片数量
		this.slideSize=this.slideItems.size();
		//保存加载图片
		this.loadNode=$(".loading-state",obj);
		//当幻灯片数量大于1的时候，初始化所有事件
		if(this.slideSize>1){
			//初始化控制按钮
			this.initControlBtns();
			this.initControlEvents();
			this.autoPlay();
		};
		this.autoPlayLoop=0;
	};
	Slide.prototype={
		//自动播放
		autoPlay:function(){
			var _this=this;
			this.autoTimer=window.setInterval(function(){
				_this.autoPlayLoop++;
				if(_this.autoPlayLoop==_this.slideSize){
					_this.autoPlayLoop=0;
				};
				_this.controlBtns.eq(_this.autoPlayLoop).mouseover();
			},5000);
		},
		//初始化控制按钮
		initControlBtns:function(){
			var _this=this;
			var slideControl=$("<ol class='slide-control'></ol>");
			//添加控制按钮
			this.slideItems.each(function(i){
					if(i==0){
						slideControl.append("<li class='control-btn selected'></li>");
					}else{
						slideControl.append("<li class='control-btn'></li>");
					};
			});
			//插入
			this.slide.append(slideControl);
			//将控制按钮保存
			this.controlBtns=slideControl.children();
			
		},
		//初始化所以控制事件
		initControlEvents:function(){
			var _this=this;
			this.controlBtns.mouseover(function(e){
				e.stopPropagation();
				var $this=this;
				this.timer=window.setTimeout(function(){
					if(!$($this).hasClass("selected")){
						_this.goto($this);
					};
				},200);
			}).mouseout(function(){
				window.clearTimeout(this.timer);
			});
			
			this.slide.mouseover(function(){
				window.clearInterval(_this.autoTimer);
			}).mouseout(function(e){
				try{
					if(e.relatedTarget.tagName=="OL"||(e.relatedTarget.tagName=="LI"&&$(e.relatedTarget).hasClass("control-btn"))){
						window.clearInterval(_this.autoTimer);
					}else{
						_this.autoPlay();
					};
				}catch(e){
					_this.autoPlay();
				};
			});	
		},
		goto:function(thisObj){
			//切换选中状态
			$(thisObj).addClass("selected").siblings().removeClass("selected");
			//切换帧
			this.switchSlide($(thisObj).index());
			//设置自动计数		
			this.autoPlayLoop=$(thisObj).index();
		},
		//选中指定帧
		switchSlide:function(index){
			var _this=this;
			var curSlide=this.slideItems.eq(index).find(".focus-bg-banner");
			var url=curSlide.attr("data-source");
			if(!curSlide.data("loaded")){
				this.loadNode.show();
				this.preLoadImg(url,function(){
					//加载完毕，执行切换
					curSlide.css("backgroundImage","url("+url+")").data("loaded",true);
					_this.fadeInShow(curSlide.parent());
					_this.loadNode.hide();
				});
			}else{
				_this.fadeInShow(curSlide.parent());
				_this.loadNode.hide();
			};
		},
		fadeInShow:function(slide){
			slide.siblings().removeClass("current").fadeOut("slow").end().addClass("current").fadeIn("slow")
		},
		preLoadImg:function(url,callBack){
			var img=new Image();
			if(!!window.ActiveXObject){
				img.onreadystatechange=function(){
					if(this.readyState=="complete"){
								callBack();
					};
				};
			}else{
				img.onload=function(){
						callBack();
				};
			};
			img.src=url;
		}
	
	};

	new Slide($("#J_Slide"));

});